/**
 * 说明：ztree树形结构简单封装
 *
 * 备注：1.该js必须放在layerui和ztree的js下方
 *      2.如果页面有2+个树形结构，使用方法如下：
 *      （1）定义一个全局变量 A
 *      （2）变量名 = new Ztree2Load(A);
 *
 * 作者：hzg
 *
 * 参数说明：
 * body ：数据位置id名称                  必传指数：☆☆☆☆☆
 * url ：数据请求地址                     必传指数：☆☆☆☆☆
 * setting ：ztree树形设置                必传指数：☆☆
 * clickFun ：点击事件函数名称             必传指数：☆☆
 * zNodes ：所有节点数据
 * ztreeObj ：ztree节点对象
 *
 * */
ztree2Load = new Ztree2Load();

function Ztree2Load($$) {
    this.body = null;
    this.url = null;
    this.clickFun = null;
    this.zNodes = null;
    this.ztreeObj = null;
    this.checkNodesId = [];
    this.setting = null;
    $$ = this;

    //初始化加载方法
    this.init = function (obj) {
        if (typeof (obj.setting) == "" || typeof (obj.setting) == "undefined") {
            obj.setting = {
                callback: {
                    onClick: obj.clickFun
                }
            };
        }
        $$.setting = (typeof (obj.setting) == "" || typeof (obj.setting) == "undefined") ? "" : obj.setting;
        $$.body = (typeof (obj.body) == "" || typeof (obj.body) == "undefined") ? "" : obj.body;
        $$.url = (typeof (obj.url) == "" || typeof (obj.url) == "undefined") ? "" : obj.url;
        if ($$.body == "" || $$.setting == "" || $$.url == "") {
            parent.layer.msg("加载树形结构失败，原因：缺少必要参数", {icon: 5, anim: 6, time: 3000});
            return false;
        }
        $$.loadInfo();
        if ($$.body.substring(0, 1) === '.' || $$.body.substring(0, 1) === '#') {
            $.fn.zTree.init($($$.body), $$.setting, $$.zNodes);
        } else {
            $.fn.zTree.init($("#" + $$.body + ""), $$.setting, $$.zNodes);
        }
        $$.ztreeObj = $.fn.zTree.getZTreeObj($$.body);
    }

    //获取select节点选中信息
    this.getSelectedNode = function () {
        return $$.ztreeObj.getSelectedNodes();
    }

    //获取checkbox节点选中信息
    this.getCheckboxNodes = function () {
        return $$.ztreeObj.getCheckedNodes(true);
    }

    //获取checkbox节点选中的所有id
    this.getCheckboxNodesId = function () {
        $$.checkNodesId = [];
        var $1 = $$.ztreeObj.getCheckedNodes(true);
        $.each($, function (n, v) {
            $$.checkNodesId.push(v.id);
        })
        return $$.checkNodesId.join(",");
    }

    //获取ztree对象
    this.loadZtreeObj = function () {
        return $$.ztreeObj;
    }

    //获取参数值
    this.loadInfo = function () {
        $.ajax({
            url: $$.url,
            type: 'POST',
            dataType: 'JSON',
            async: false,
            success: function (result) {
                if (result.code == undefined) {
                    result = JSON.parse(result);
                }
                if (result.code == 0) {
                    $$.zNodes = result.data;
                } else {
                    parent.layer.msg(result.msg, {icon: 5, anim: 6, time: 3000});
                }
            },
            error: function (e) {
                parent.layer.msg('系统异常，请稍后重试！', {icon: 5, anim: 6, time: 3000});
            }
        });
    }
}